<template>
  <div>
    <div class="pageTopTitle">渠道管理</div>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
        <el-form-item label="账户名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入账户名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="账户号" prop="account">
          <el-input
            v-model="queryParams.account"
            placeholder="请输入账户号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="app-container mt20">
      <el-row :gutter="10" class="mb20">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="accountList">
        <el-table-column label="账户名称" align="center" prop="name"/>
        <el-table-column label="账户号" align="center" prop="account"/>
        <el-table-column label="开户行" align="center" prop="bankName"/>
        <el-table-column label="开户行地址" align="center" prop="bankAddress"/>
        <el-table-column label="税号" align="center" prop="taxId"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改收款账户对话框 -->
      <el-drawer
        :title="title"
        :visible.sync="open"
        size="35%"
        :wrapper-closable="false"
      >
        <div class="drawer-content">
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="账户名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入账户名称"/>
            </el-form-item>
            <el-form-item label="账户号" prop="account">
              <el-input v-model="form.account" placeholder="请输入账户号"/>
            </el-form-item>
            <el-form-item label="开户行" prop="bankName">
              <el-input v-model="form.bankName" placeholder="请输入开户行"/>
            </el-form-item>
            <el-form-item label="开户行地址" prop="bankAddress">
              <el-input v-model="form.bankAddress" placeholder="请输入开户行地址"/>
            </el-form-item>
            <el-form-item label="税号" prop="taxId">
              <el-input v-model="form.taxId" placeholder="请输入税号"/>
            </el-form-item>
          </el-form>
        </div>
        <div class="drawer-footer">
          <el-button type="primary" size="small" @click="submitForm">确 定</el-button>
          <el-button @click="cancel" size="small">取 消</el-button>
        </div>
      </el-drawer>
    </div>
  </div>
</template>

<script>
import { listAccount, getAccount, delAccount, addAccount, updateAccount } from '@/api/web/account'

export default {
  name: 'Account',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收款账户表格数据
      accountList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        account: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: '账户名称不能为空', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '账户号不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询收款账户列表 */
    getList() {
      this.loading = true
      listAccount(this.queryParams).then(response => {
        this.accountList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        name: null,
        account: null,
        bankName: null,
        bankAddress: null,
        taxId: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加收款账户'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getAccount(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改收款账户'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAccount(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addAccount(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除收款账户编号为"' + ids + '"的数据项？').then(function() {
        return delAccount(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.drawer-content {
  padding: 0 30px 80px 20px;
}

.drawer-footer {
  position: absolute;
  bottom: 0;
  box-sizing: border-box;
  text-align: right;
  padding: 16px;
  width: 100%;
  border-top: 1px solid #eee;
  background-color: #fff;
}

::v-deep .el-form-item__label {
  font-weight: 500 !important;
}

::v-deep .el-form--label-top .el-form-item__label {
  padding: 0 0 4px 0 !important;
}
</style>

